import {Component, ViewEncapsulation, ViewChild} from '@angular/core';
import {IdeaService} from '../../services/app.idea';
import {Ideas} from '../shared/ideas/idea.component';
import {UserService} from '../../services/app.user';
import {BaThemeSpinner}  from '../../theme/services';
declare let toastr:any;
declare let $:any;

@Component({
    selector: 'allTime',
    encapsulation: ViewEncapsulation.None,
    templateUrl: './allTime.html',
    styleUrls: ['./allTime.scss']
})
export class allTime {
    @ViewChild('ideas')
    public idea:Ideas;
    public totalIdeasDeatail = {
        skipItem: 0,
        itemsperPage: 20,
        sortParameter: 'points',
        sortOrder: 'descending',
        toDate: '01/01/2009',
        fromDate: '01/01/2008'
    };
    public start:any;
    public end:any;
    public check:any;
    public versioning:boolean;
    public totalIdeas:any;

    constructor(private spinner:BaThemeSpinner, public ideaService:IdeaService, public userService:UserService) {
        this.start = 0;
        this.check = 0;

        // Function call for start timeline count
        this.startCount();
        // API call for get user info
        this.userService.getuserInfo().subscribe(
            data => this.userInfoSuccess(data),
            error => this.userInfoFail(error)
        );
        this.versioning = false;
    };

    ngAfterViewInit() {
        this.getRecentIDeas();
    }

    // Function for get recent idea API call
    public getRecentIDeas() {
        this.spinner.showProgress();
        this.ideaService.ideaList(this.totalIdeasDeatail).subscribe(
            data => this.success(data),
            error => this.fail(error)
        );
    }

    // Function for check version lock for user
    public userInfoSuccess(data) {
        console.log("all time");
        if (data.version === 'Version_1') {
            this.versioning = false;
        } else if (data.version === 'Version_2') {
            this.versioning = false;
        } else if (data.version === 'Version_4') {
            this.versioning = false;
        } else if (data.version === 'Version_6') {
            this.versioning = false;
        } else {
            this.versioning = true;
        }
    }

    // Function call if user info API fail
    public  userInfoFail(err) {
    }

    // Function call when get recent idea success to load
    public success(result) {
        if (this.versioning) {
            for (let x = 0; x < result.ideaList.length; x++) {
                if (result.ideaList[x].citationArray !== undefined) {
                    for (let y = 0; y < result.ideaList[x].citationArray.length; y++) {
                        result.ideaList[x].content = result.ideaList[x].content.replace(result.ideaList[x].citationArray[y], '<span class="keywordColor">' + result.ideaList[x].citationArray[y] + '</span>');
                    }
                }
            }
        }

        this.spinner.hideProgress(3500);
        this.totalIdeas = result;
        this.idea.setIdeasInMasonry(result);
    }

    // Function call when get recent idea fail to load
    public fail(error) {
        this.spinner.hideProgress();
    }

    // Function for show more idea on click show more button
    public showMoreIdea() {
        this.totalIdeasDeatail.skipItem += this.totalIdeasDeatail.itemsperPage;
        this.getRecentIDeas();
    }

    // To call every 1 sec for count how much time we spent on this page
    public  timedCount() {
        this.start = this.start + 1;
        this.end = setTimeout(() => {
            this.timedCount()
        }, 1);
    }

    // To function for start timstamp count
    public startCount() {
        if (!this.check) {
            this.check = 1;
            this.timedCount();
        }
    }

    // To function for stop timstamp count
    public stopCount() {
        clearTimeout(this.end);
        this.check = 0;
    }

    // To call this function when leave from this controller
    ngOnDestroy() {
        // To stop count function call
        this.stopCount();

      // API call for tract navigation sequence
      let seq = {
        sequence: {
          "pageName": 'topAllTime',
          "timeSpent": this.start
        }

      };
      this.userService.setUrlSeq(seq).subscribe();
    }
}

